package com.zq.server.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zq.common.constant.NumberConstants;
import com.zq.common.core.domain.Result;
import com.zq.common.utils.SecurityUtils;
import com.zq.server.admin.dao.SysNotifyMapper;
import com.zq.server.admin.domain.SysActivityNotify;
import com.zq.server.admin.domain.SysNotify;
import com.zq.server.admin.service.ISysActivityNotifyService;
import com.zq.server.admin.service.ISysNotifyService;

/**
 * 操作日志 服务层处理
 * 
 * @author 财哥
 */
@Service
public class ISysNotifyServiceImpl implements ISysNotifyService {
    @Autowired
    private SysNotifyMapper notifyMapper;

    @Autowired
    private ISysActivityNotifyService sysActivityNotifyService;

    /**
     * 新增
     * 
     * @param notify 操作日志对象
     */
    @Override
    public void insertNotify(SysNotify notify) {
        notifyMapper.insertNotify(notify);
    }

    /**
     * 查询系统操作日志集合
     * 
     * @param notify 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public List<SysNotify> selectNotifyList(SysNotify notify) {
        notifyMapper.updateNotifyList(SecurityUtils.getWechatUserId());
        return notifyMapper.selectNotifyList(notify);
    }

    /**
     * 批量删除系统操作日志
     * 
     * @param notifyIds 需要删除的操作日志ID
     * @return 结果
     */
    @Override
    public int deleteNotifyByNotifyIds(Long[] notifyIds) {
        return notifyMapper.deleteNotifyByNotifyIds(notifyIds);
    }

    /**
     * 查询操作日志详细
     * 
     * @param notifyId 操作ID
     * @return 操作日志对象
     */
    @Override
    public SysNotify selectNotifyById(Long notifyId) {
        return notifyMapper.selectNotifyById(notifyId);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanNotify() {
        notifyMapper.cleanNotify();
    }

    /**
     * @return com.zq.common.core.domain.Result 获取用户未阅读的消息通知
     * @author 财哥 &#064; 2024.3.30 16:49
     **/
    @Override
    public Result getUnreadMessageCount() {

        Map<String, Object> map = new HashMap<>();

        // 查询获取 SysNotify 对象
        List<SysNotify> response =  notifyMapper.getSysNotifyByUid(SecurityUtils.getWechatUserId());
        // 获取状态为1的消息数量
        // 计算状态为1的消息数量
        long unreadMessagesCount = response.stream()
                .filter(sysNotify -> sysNotify.getStatus() == NumberConstants.ONE_)
                .count();
        // 构造 SysActivityNotify 对象
        SysActivityNotify sysActivityNotify = new SysActivityNotify();
        sysActivityNotify.setTargetId(SecurityUtils.getWechatUserId());
        sysActivityNotify.setStatus(NumberConstants.ONE_);
        // 调用 service 方法获取活动通知数量
        Integer activityCount = sysActivityNotifyService.selectNotifyByIdCount(sysActivityNotify);

        // 将结果放入 map 中
        map.put("sysNotify", response != null ? response : new SysNotify());
        map.put("activityCount", activityCount != null ? activityCount : 0);
        map.put("sysNotifyCount",unreadMessagesCount);
        // 返回结果
        return Result.success(map);

    }
}
